package fonkin.com.wifisignapp.acticity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.EditText;

import com.squareup.picasso.Picasso;

import java.util.Timer;
import java.util.TimerTask;

import de.hdodenhof.circleimageview.CircleImageView;
import fonkin.com.wifisignapp.R;
import fonkin.com.wifisignapp.bean.user.User;
import fonkin.com.wifisignapp.constants.AppConstants;
import fonkin.com.wifisignapp.fragment.MainFragment;
import fonkin.com.wifisignapp.retrofit.BaseRetrofit;
import fonkin.com.wifisignapp.retrofit.ServiceApis;
import fonkin.com.wifisignapp.utils.MD5Util;
import fonkin.com.wifisignapp.utils.PrintLog;
import fonkin.com.wifisignapp.utils.ReturnVo;
import fonkin.com.wifisignapp.utils.ShowToast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class LoginActivity extends AppCompatActivity implements Callback<ReturnVo<User>> {

    EditText login_name,login_pwd;
    CheckBox cb_checkbox;
    ProgressDialog loading=null;

    private final long SPLASH_LENGTH = 2500;
    Handler handler = new Handler();
    SharedPreferences pref;
    SharedPreferences.Editor editor;
    CircleImageView circleImageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN ,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_login);
        login_name = (EditText) findViewById(R.id.login_name);
        login_pwd = (EditText) findViewById(R.id.login_pwd);
        cb_checkbox = (CheckBox) findViewById(R.id.cb_checkbox);
        circleImageView = (CircleImageView)findViewById(R.id.iv_icon);

        pref = PreferenceManager.getDefaultSharedPreferences(this);
        if (pref.contains("user_image")&&!"".equals(pref.getString("user_image",""))){
            Picasso
                    .with(this)
                    .load(AppConstants.IMAGE_API + pref.getString("user_image",""))
                    .error(R.drawable.un_head_pic)
                    .fit()
                    .centerCrop()
                    .into(circleImageView);
        }else {
            circleImageView.setImageResource(R.drawable.un_head_pic);
        }
        if (pref.contains("remember_pwd")){
            if (pref.getBoolean("remember_pwd",false)) {
                if (pref.contains("user_name") && pref.contains("user_pwd")) {
                    login_name.setText(pref.getString("user_name",""));
                    login_pwd.setText(MD5Util.convertMD5(pref.getString("user_pwd","")));
                }
            }
        }

    }

    public void initRetrofit(String name,String pwd) {
        ServiceApis serviceApis = BaseRetrofit.GetBaseRetrofit();
        Call<ReturnVo<User>> voCall = serviceApis.login(name, pwd);
        voCall.enqueue(this);
    }


    public void Login(View v){
        final String lo_name = login_name.getText().toString();
        final String lo_pwd = login_pwd.getText().toString();
        if ("".equals(lo_name)||"".equals(lo_pwd)){
            ShowToast.ShowToastShort("请输入完整信息！！",LoginActivity.this);
        }
        else{

            //登录时加载进度弹窗
            handler.postDelayed(new Runnable() {  //使用handler的postDelayed实现延时跳转

                public void run() {
                    initRetrofit(lo_name,lo_pwd);
                }


            }, SPLASH_LENGTH);
            loading = ProgressDialog.show(this,"Loging","正在登录！！",false,false);
        }
    }


    @Override
    public void onResponse(Call<ReturnVo<User>> call, Response<ReturnVo<User>> response) {
        if (response.isSuccessful()){
            Integer code = response.body().getCode();

            //打印接口调用情况和接口获取到的数据
            PrintLog.printApi(response,call);
            if(code.equals(AppConstants.SUCCESS)){
                User user = response.body().getData();
                 loading.cancel();
                editor=pref.edit();
                editor.putString("user_image",user.getHeadImg());
                editor.putString("user_name",user.getName());
                editor.putString("dept_name",user.getDeptName());
                editor.putString("user_pwd",user.getPwd());
                editor.putString("user_age",user.getAge());
                editor.putString("user_tel",user.getTel());
                editor.putString("user_email",user.getEmail());
                editor.putString("user_sex",user.getSex());
                editor.putString("user_id",user.getId());
                if (cb_checkbox.isChecked()){
                    editor.putBoolean("remember_pwd",cb_checkbox.isChecked());
                }
                editor.apply();
                ShowToast.ShowToastShort("登陆成功！欢迎"+user.getName(),LoginActivity.this);
                Intent intent = new Intent(LoginActivity.this,MainFragment.class);
                startActivity(intent);
                finish();
            }else{
                loading.cancel();
                ShowToast.ShowToastShort("用户不存在！请重新登录",LoginActivity.this);
            }
        }else{
            ShowToast.ShowToastShort(AppConstants.Fail_REQUEST,LoginActivity.this);
            loading.cancel();
        }
    }

    @Override
    public void onFailure(Call<ReturnVo<User>> call, Throwable t) {
        ShowToast.ShowToastShort(AppConstants.Fail_RESPONSE,LoginActivity.this);
        loading.cancel();
    }
}
